using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using JetBrains.Annotations;

namespace SG.Productivity.DesktopControlPanel.Support
{
    public enum DataType
    {
        [UsedImplicitly] None = 0,
        Document,
        Detail,
        Address,
        Activity,
    }

    public enum Action
    {
        [UsedImplicitly] None = 0,
        Display
    }

    public class ProjectPointer
    {
        private readonly ProjectType _type;
        private readonly int _year;
        private readonly int _number;

        public ProjectPointer(ProjectType type, int completeNumber)
        {
            _type = type;
            _year = Math.DivRem(completeNumber, _type.Factor10(), out _number);
        }

        public ProjectPointer(ProjectType type, int year, int number)
        {
            _type = type;
            _year = year;
            _number = number;
        }

        public ProjectType ProjectType { get { return _type; } }

        public int Year { get { return _year; } }

        public int Number { get { return _number; } }

        public int CompleteNumber
        {
            get { return (Year* _type.Factor10()) + Number; }
        }

        public IEnumerable<string> DataFolder
        {
            get
            {
                yield return ProjectType.Plural().ToString();

                if (ProjectType.HasYearComponent())
                    yield return Year.ToString(CultureInfo.InvariantCulture);

                yield return FormattedNumber;
            }
        }

        public string AbsoluteDataFolder
        {
            get { return Path.Combine(ProjectType.PathPrefix(), Path.Combine(DataFolder.ToArray())); }
        }

        public static implicit operator int(ProjectPointer pointer)
        {
            return pointer.CompleteNumber;
        }

        public Uri ToUri(IParameterCollection queryItems)
        {
            return queryItems.ToUri((ProjectType.HasYearComponent() ? Year + "/" : "") + FormattedNumber,
                                    scheme: ProjectType.Singular().ToString().ToLowerInvariant());
        }

        private string FormattedNumber
        {
            get { return Number.ToString(CultureInfo.InvariantCulture).PadLeft(ProjectType.Factor(), '0'); }
        }
    }
}